package com.youxin.foundation.utils;

import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.HostAndPort;

/**
 * Host主机地址处理工具类
 * @author yingde.cao
 *
 */
public class HostUtils {
	
	public static Set<HostAndPort> parserHosts(String hosts) throws ParseException
	{
		if (hosts == null || hosts.length() == 0) {
			throw new ParseException("hosts is null.",0);
		}
		Set<HostAndPort> haps = new HashSet<>();
		String[] nodes=hosts.split(",", 0);
		
		for (String node : nodes) {
			node=node.trim();
			if(node.length()==0){
				continue;
			}
			HostAndPort hostAndPort=parserHost(node);			
			haps.add(hostAndPort);
		}
		return haps;
	}
	private static int HOST_PORT_LENGTH=2;
	public static HostAndPort parserHost(String host) throws ParseException
	{
		String[] arr = host.split(":");
		if (arr.length != HOST_PORT_LENGTH) {
			throw new ParseException("node address "+host+" error !",0);
		}
		return new HostAndPort(arr[0].trim(), Integer.valueOf(arr[1].trim()));
	}
}
